package com.example.chapter9sqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    MyDbOpenHelper myDbOpenHelper;
    SQLiteDatabase readableDatabase;
    SQLiteDatabase writableDatabase;
    private ListView listView;
    private EditText etStuId,etStuName,etStuClass;
    private Button btnInsert,btnUpdate,btnDelete;
    String currentId;
    private String url = "content://com.example.chapter9sqlite.provider";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         myDbOpenHelper = new MyDbOpenHelper(getApplicationContext(), "cy.db", null, 1);
        writableDatabase = myDbOpenHelper.getWritableDatabase();
        readableDatabase = myDbOpenHelper.getReadableDatabase();
         listView = findViewById(R.id.listview);
        etStuId = findViewById(R.id.et_stuid);
        etStuName = findViewById(R.id.et_stuname);
        etStuClass = findViewById(R.id.et_stuclass);

        btnInsert = findViewById(R.id.btn_insert);
        btnUpdate = findViewById(R.id.btn_update);
        btnDelete = findViewById(R.id.btn_del);
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase writableDatabase = myDbOpenHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuId",Integer.parseInt(etStuId.getText().toString()));
                contentValues.put("stuName",etStuName.getText().toString());
                contentValues.put("stuClass",etStuClass.getText().toString());
                writableDatabase.insert("student","stuId",contentValues);
                showAll();
            }
        });
        btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase writableDatabase = myDbOpenHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuId",Integer.parseInt(etStuId.getText().toString()));
                contentValues.put("stuName",etStuName.getText().toString());
                contentValues.put("stuClass",etStuClass.getText().toString());
                writableDatabase.update("student",contentValues,"_id=?",new String[]{currentId});
                showAll();
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                writableDatabase.delete("student",null,null);
                showAll();
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView tvId = view.findViewById(R.id.tv_id);
                TextView tvStuid = view.findViewById(R.id.tv_stuid);
                TextView tvName = view.findViewById(R.id.tv_name);
                TextView tvClass = view.findViewById(R.id.tv_class);
                currentId = tvId.getText().toString();
                etStuId.setText(tvStuid.getText().toString());
                etStuName.setText(tvName.getText().toString());
                etStuClass.setText(tvClass.getText().toString());
                Log.e("TAG", "onItemClick: "+currentId );
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                TextView tvId = view.findViewById(R.id.tv_id);
                currentId = tvId.getText().toString();
                TextView tvName = view.findViewById(R.id.tv_name);
                String itemname = tvName.getText().toString();
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("标题")
                        .setMessage("信息内容，你要删除吗  "+itemname)
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                writableDatabase.delete("student","_id=?",new String[]{currentId});
                                showAll();
                            }
                        }).show();
                return true;
            }
        });
        showAll();
    }
    private void showAll(){

        Log.e("TAG", "showAll: "+getContentResolver().toString());
        Cursor query = getContentResolver().query(Uri.parse(url), new String[]{"_id","stuId", "stuName", "stuClass"}, null, null, "stuId");
//        Cursor query = readableDatabase.query("student", null, "stuClass=?", new String[]{"数媒1993"}, null, null, "stuId");
//        Cursor query = readableDatabase.query("student", null, null, null, null, null, "stuId");
        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(getApplicationContext(),
                R.layout.list_item, query,
                new String[]{"_id","stuId", "stuName", "stuClass"},
                new int[]{R.id.tv_id,R.id.tv_stuid, R.id.tv_name, R.id.tv_class},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(simpleCursorAdapter);
    }
}